/*
 * Copyright 2007 Gergely Kis
 * Copyright 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.user.client.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.impl.ElementImpl;
import com.google.gwt.user.client.impl.EventImpl;
import com.gwthtml.servlet.GWTContext;

/**
 * A standard push-button widget.
 * 
 * <p>
 * <img class='gallery' src='Button.png'/>
 * </p>
 * 
 * <h3>CSS Style Rules</h3>
 * <ul class="css">
 * <li>.gwt-Button { }</li>
 * </ul>
 * 
 * <p>
 * <h3>Example</h3> {@example com.google.gwt.examples.ButtonExample}
 * </p>
 */
public class Button extends ButtonBase {
	
  static void click(Element button) {
    // FIXME: Move this to GWTContext or DOMImpl?
    EventImpl evt = new EventImpl(Event.ONCLICK | Event.BUTTON_LEFT);
    evt.setTarget(button);
    GWTContext.emitEvent(evt);
  }

  static void adjustType(Element button) {
    ElementImpl e = (ElementImpl) button;
	e.setAttribute("type", "submit");
	e.setAttribute("name", "click");
	e.setAttribute("value", ""+button.hashCode());
  }

  /**
   * Creates a button with no caption.
   */
  public Button() {
    super(DOM.createButton());
    adjustType(getElement());
    setStyleName("gwt-Button");
    setActiveElement(true);
  }

  /**
   * Creates a button with the given HTML caption.
   * 
   * @param html the HTML caption
   */
  public Button(String html) {
    this();
    setHTML(html);
  }

  /**
   * Creates a button with the given HTML caption and click listener.
   * 
   * @param html the HTML caption
   * @param listener the click listener
   */
  public Button(String html, ClickListener listener) {
    this(html);
    addClickListener(listener);
  }

  /**
   * Programmatic equivalent of the user clicking the button.
   */
  public void click() {
    click(getElement());
  }

  public void setHTML(String html) {
	DOM.setInnerHTML(getElement(), html);
  }
  
}
